package com.hqyj.hrms.modules.personnel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.hrms.modules.personnel.dao.ContractMapper;
import com.hqyj.hrms.modules.personnel.entity.Contract;
import com.hqyj.hrms.modules.personnel.entity.Employee;
import com.hqyj.hrms.modules.personnel.entity.Record;
import com.hqyj.hrms.modules.personnel.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Autor zyj
 * @Date 2021/10/2
 **/
@Service
public class ContractServiceImpl implements ContractService {
    //注入mapper
    @Autowired(required = false)
    ContractMapper contractMapper;

    @Override
    public HashMap<String, Object> list(Contract contract) {
        HashMap<String, Object> map = new HashMap<String, Object>();
//        //创建查询插件对象
//        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
//
//        //条件   列名   参数
//        queryWrapper.eq(
//                employee.getEmpName() != null && !employee.getEmpName().equals(""),
//                "emp_name",
//                employee.getEmpName());

        String keyWord = contract.getKeyWord();
        QueryWrapper wrapper = new QueryWrapper<Contract>()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "contract_id", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "emp_id", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "contract_startday", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "contract_endday", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "enp_job", keyWord).or()
                .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(keyWord), "contract_synopsis", keyWord).or()
                .like(StringUtils.isNotBlank(keyWord), "contract_remarks", keyWord);

        //第一步创建分页对象
        Page<Contract> contractPage = new Page<>(contract.getPage(), contract.getRow());
        //第二页  查询分页  获取分页数据集
        Page<Contract> pageList = contractMapper.selectPage(contractPage, wrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
//        List<UserInfo> list = userInfoMapper.selectList(null);
//        map.put("info",list);
        return map;
    }

    @Override
    public HashMap<String, Object> add(Contract contract) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = contractMapper.insert(contract);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Contract contract) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = contractMapper.updateById(contract);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = contractMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }
}
